### Creates Appendix Figure B1: refugees in Canada and other G7 countries 

library(readr)
library(data.table)

setwd()

asylum <- read_csv("Raw Data/asylum.csv")
asylum <- data.table(asylum)

asylum <- asylum[Year>=1980 & Year<=2006 & Type %in% c("REF", "ASY")]
G7 <- asylum[Asylum %in% c("France", "Germany", "Italy", "Japan", "United States of America", "United Kingdom", "Australia"),
             .(N=sum(N)), by=c("Asylum", "Year")]
G7 <- G7[order(Asylum, Year)]

canada <- read_csv("~/canada.csv")
canada <- data.table(canada)
canada <- canada[Year>=1980 & Year<=2006]

pop <- read_csv("~/world population.csv")
pop <- data.table(pop)
pop <- pop[SUBJECT=="TOT"]
G7$Country <- NULL
G7$Country[which(G7$Asylum=="United States of America")] <- "USA"
G7$Country[which(G7$Asylum=="France")] <- "FRA"
G7$Country[which(G7$Asylum=="Germany")] <- "DEU"
G7$Country[which(G7$Asylum=="Italy")] <- "ITA"
G7$Country[which(G7$Asylum=="Japan")] <- "JPN"
G7$Country[which(G7$Asylum=="United Kingdom")] <- "GBR"
G7$Country[which(G7$Asylum=="Australia")] <- "AUS"
G7 <- merge(x=G7, y=pop, by.x=c("Country", "Year"), by.y=c("LOCATION", "TIME"))
canada <- merge(x=canada, y=pop[LOCATION=="CAN"], by.x="Year", by.y="TIME")
G7$N_pp <- G7$N/(G7$Value); canada$N_pp <- canada$Refugees/(canada$Value)


par(mar=c(4.5,4.7,2,2))
plot(canada[, c("Year", "Refugees")], type="h", lwd=25, lend=1, col="grey40", ylab="Number of Refugees", cex.lab=1.5, cex.axis=1.5)

plot(G7[Asylum=="United States of America", c("Year", "N_pp")], ylim=c(0,5500), type="o", col="red", lwd=2, pch=17, ylab="Refugees Per 1,000,000", cex.lab=1.5, cex.axis=1.5)
points(canada[, c("Year", "N_pp")], type="o", lwd=2, pch=19)
points(G7[Asylum=="France", c("Year", "N_pp")], type="o", col="royalblue", lwd=2, pch=23)
points(G7[Asylum=="Germany", c("Year", "N_pp")], type="b", col="darkorange", lwd=2, pch=22)
points(G7[Asylum=="Italy", c("Year", "N_pp")], type="o", col="forestgreen", lwd=2, pch=15)
points(G7[Asylum=="United Kingdom", c("Year", "N_pp")], type="b", col="seagreen2", lwd=2, pch=25)
points(G7[Asylum=="Australia", c("Year", "N_pp")], type="b", col="grey40", pch=19, lwd=2)
legend(x=2002, y=5600, col=c("darkorange", "black", "royalblue", "seagreen2", "grey40", "red", "darkgreen"), pch=c(22, 19, 23, 25, 19, 17, 15), lwd=c(2,2,2,2,2,2,2), 
       c("Germany", "Canada", "France", "UK", "Australia", "United States", "Italy"), bty="n", cex=1.2)

